package cn.smbms.service;

import java.io.File;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.pojo.Provider;
import cn.smbms.dao.ProviderDao;

@Service
public class ProviderServiceImpl implements ProviderService {
	@Autowired
	private ProviderDao pdi;
	@Autowired
	private BillService bsi;
	@Override
	public List<Provider> getListAll(String code,String name) {
		return pdi.getListAll(code,name);
	}

	@Override
	public int addProvider(Provider pro) {
		return pdi.addProvider(pro);
	}

	@Override
	public Provider getById(String id) {
		return pdi.getById(id);
	}

	@Override
	public int providerUp(Provider pro) {
		return pdi.providerUp(pro);
	}

	@Override
	public int delProvider(String proId) {
		int num=0;
		int index=0;
		if(bsi.BillByProId(proId)>0){
			num=-1;
		}else{
			String fileName1=pdi.getById(proId).getCodePicPath();
			String fileName2=pdi.getById(proId).getLicPicPath();
			if(!(fileName1==null||fileName1.equals(""))){
				index=fileName1.lastIndexOf("\\");
				String name=fileName1.substring(index+1);
				File file=new File("D:\\Program Files\\bdqn\\Y2\\EclipseWorkspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\ch10_2\\statics\\uploadFile\\"+name);
				file.delete();
			}
			if(!(fileName2==null||fileName2.equals(""))){
				index=fileName2.lastIndexOf("\\");
				String name=fileName2.substring(index+1);
				File file=new File("D:\\Program Files\\bdqn\\Y2\\EclipseWorkspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\ch10_2\\statics\\uploadFile\\"+name);
				file.delete();
			}
			num=pdi.delProvider(proId);
		}
		return num;
	}

	@Override
	public List<Provider> getList() {
		return pdi.getList();
	}

}
